<?php
/* 接收json数据 */
require("../../utils/resolve_json.php");
/* sql操作工具 */
require("../../utils/tools.php");
/* 登录验证 */
require("../security/existLogin.php");
/* 获取作者的Token */
$author_token = Security::get_requst_token();

if (!isset($data["name"])) {
    $code = 3000;
    $msg = "发布逗逗失败,逗逗名称不能为空!";
} else {
    if (!isset($data["content"])) {
        $code = 3001;
        $msg = "发布逗逗失败,逗逗的内容不能为空!";
    } else {

        if (!isset($data["category"])) {
            $code = 3003;
            $msg = "发布逗逗失败,逗逗的分类不能为空!";
        } else {
            if (!SQLTools::existTB($conn, 'categories', "cg_uuid='${data["category"]}'")) {
                $code = 3004;
                $msg = "发布逗逗失败,指定的分类不存在!";
            } else {
                $uuid = SQLTools::$SQL_UUID;
                if (SQLTools::insertTB($conn, 'jokes', 'jk_uuid,jk_name,jk_content,jk_author,jk_category,jk_create_time,jk_update_time', "$uuid,'${data["name"]}','${data["content"]}','$author_token','${data["category"]}',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP")) {
                    $code = 2000;
                    $msg = "发布逗逗成功!";
                } else {
                    $code = 3033;
                    $msg = "发布逗逗失败,原因未知!";
                }
            }
        }
    }
}
$result = array("code" => $code, "msg" => $msg, "author" => $author_token, "data" => $data);
echo json_encode_mz_urlencode($result);
